package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.datas.PlaygroundData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * http:\/\/userimg.shiwan.com\/uploads\/manage\/6d\/3f\/9d91a9bdaa4ad7fc75bb1ee8ea9e05e022ca.jpg
 * http:\/\/userimg.shiwan.com\/uploads\/manage\/e2\/8a\/86bcd4aa21cbcd9a48c89575ad63830518c0.jpg
 * Created by dllo on 16/1/21.
 */
public class PlaygroundAdapter extends RecyclerView.Adapter<PlaygroundAdapter.PlaygroundViewHolder> {
    private Context context;
    private List<PlaygroundData.DataEntity.ResultsEntity> photos;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private OnRecyclerItemClickListener onRecyclerItemClickListener;
    public PlaygroundAdapter(Context context) {
        this.context = context;
        photos = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    public void addData(List<PlaygroundData.DataEntity.ResultsEntity> photos) {
        this.photos = photos;
        notifyDataSetChanged();
    }

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener) {
        this.onRecyclerItemClickListener = onRecyclerItemClickListener;
    }

    @Override
    public PlaygroundViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_playground, parent, false);
        return new PlaygroundViewHolder(v);
    }

    @Override
    public void onBindViewHolder(PlaygroundViewHolder holder, final int position) {
        holder.networkImageView.setDefaultImageResId(R.mipmap.planmain320_370);
        holder.networkImageView.setErrorImageResId(R.mipmap.planmain320_370);
        holder.networkImageView.setImageUrl(photos.get(position).getPhoto(), imageLoader);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRecyclerItemClickListener!=null){
                    onRecyclerItemClickListener.onItemClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return photos.size();
    }

    class PlaygroundViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView networkImageView;
        RelativeLayout playground;

        public PlaygroundViewHolder(View itemView) {
            super(itemView);
            networkImageView = (NetworkImageView) itemView.findViewById(R.id.playgroundNI);
            playground = (RelativeLayout) itemView.findViewById(R.id.playground);
        }
    }
}
